package room;

import items.Item;

import java.util.ArrayList;
import java.util.StringTokenizer;

public class Room
{
  private static int nextID = 0;
  private int id;
  private String name = "";
  private ArrayList<RoomState> states;
  private RoomState currentState;
  private ArrayList<Item> items;
  
  public Room()
  {
    this("");
  }
  
  /**
   * @param description
   */
  public Room(String name)
  {
    this.id = nextID++;
    this.name = name;

    states = new ArrayList<RoomState>();
    currentState = new RoomState();
    states.add(currentState);
    
    items = new ArrayList<Item>();
  }

  public Item createItem()
  {
    Item newItem = new Item();
    items.add(newItem);
    return newItem;
  }
  
  public RoomState createRoomState()
  {
    RoomState newState = new RoomState();
    states.add(newState);
    return newState;
  }
  
  public String executeVerb(String verb)
  {
    return this.currentState.executeVerb(verb);
  }
  
  public String useItem(Item item)
  {
    return this.currentState.useItem(item);
  }
  
  public String toString()
  {
    String ret = "[" + id + ", "+ name + ", [";
    for(RoomState rs : states)
      ret += rs.toString() + ", ";
    ret += "] ]";
    return ret;
  }
  
  /**
   * @return the items
   */
  public ArrayList<Item> getItems()
  {
    return items;
  }

  public int hashCode()
  {
    return id;
  }
  
  /**
   * @return the id
   */
  public int getId()
  {
    return id;
  }

  /**
   * @param id the id to set
   */
  public void setId(int id)
  {
    this.id = id;
  }
  
  /**
   * @return the name
   */
  public String getName()
  {
    return name;
  }

  /**
   * @param name the name to set
   */
  public void setName(String name)
  {
    this.name = name;
  }

  /**
   * @return the states
   */
  public ArrayList<RoomState> getStates()
  {
    return states;
  }
  
  /**
   * @return the currentState
   */
  public RoomState getCurrentState()
  {
    return currentState;
  }

  /**
   * @param currentState the currentState to set
   */
  public String setCurrentState(RoomState currentState)
  {
    this.currentState = currentState;
    return this.currentState.executeVerb("onEnterState");
  }
  
  /**
   * @param currentState the currentState to set
   */
  public String setCurrentState(int stateID)
  {
    for(RoomState rs : states)
      if(rs.getId() == stateID)
        return setCurrentState(rs);
    return "";
  }
  
  public RoomState getRoomState(int stateID)
  {
    for(RoomState rs : states)
      if(rs.getId() == stateID)
        return rs;
    return null;
  }
  
}